import 'package:flutter/material.dart';
import 'package:x3_component_core/theme/base_button_theme.dart';

class BaseDialogComponent extends StatelessWidget {

  /// 标题文字
  final String title;

  /// 标题布局
  final Widget titleWidget;

  /// 内容标题
  final String content;

  /// 内容布局
  final Widget contentWidget;

  /// 确定按钮的点击事件
  final VoidCallback onOKPress;

  /// 关闭按钮的点击事件
  final VoidCallback onCancelPress;

  /// 构造函数
  BaseDialogComponent({Key key, this.title, this.titleWidget, this.content, this.contentWidget, @required this.onOKPress, this.onCancelPress}) : super(key: key);

  /// default Border
  static const RoundedRectangleBorder _defaultDialogShape = RoundedRectangleBorder(borderRadius: BorderRadius.all(Radius.circular(5.0)));

  @override
  Widget build(BuildContext context) {
    return Dialog(
      shape: _defaultDialogShape,
      child: _buildDialogChild(context),
    );
  }

  /// 构建弹出框子布局
  Widget _buildDialogChild(BuildContext context) {
    return IntrinsicWidth(
      child: Column(
        mainAxisSize: MainAxisSize.min,
        crossAxisAlignment: CrossAxisAlignment.stretch,
        children: _buildDialogChildren(context),
      ),
    );
  }

  List<Widget> _buildDialogChildren(BuildContext context) {
    return [
      _buildTitleWidget(context),
      _buildContentWidget(context),
      _buildDialogActions(context),
    ];
  }

  Widget _buildTitleWidget(BuildContext context) {
    if (this.titleWidget != null) return this.titleWidget;
    if (this.title != null) return Container(height: 44, alignment: Alignment.centerLeft, padding: EdgeInsets.symmetric(horizontal: 12), child: Text(this.title));
    return SizedBox();
  }

  Widget _buildContentWidget(BuildContext context) {
    final content = this.content == null ? '内容提示文本' : this.content;
    return Container(
      constraints: BoxConstraints(minHeight: 70),
      padding: EdgeInsets.symmetric( horizontal: 12.0, vertical: 0 ),
      alignment: Alignment.center,
      child: this.contentWidget != null ? contentWidget : Text(content, style: Theme.of(context).textTheme.bodyText2.copyWith(fontSize: 15)),
    );
  }

  Widget _buildDialogActions(BuildContext context) {
    final Color color = Theme.of(context).textTheme.bodyText2.color;
    final sureColor = this.onOKPress == null ? Colors.grey : color;
    return Column(
      children: [
        Divider(height: 1.0),
        Container(
          height: 48.0,
          child: Row(
            children: [
              _buildDialogActionItem(context, '取消', () => this._onCloseButtonPress( context )),
              VerticalDivider(width: 1.0),
              _buildDialogActionItem(context, '确定', () => this._onSureButtonPress( context ), color: sureColor),
            ],
          ),
        ),
      ],
    );
  }

  Widget _buildDialogActionItem(BuildContext context, String text, Function onPress, { Color color }) {
    return Expanded(
      child: InkWell(
        onTap: onPress,
        child: Container(
          alignment: Alignment.center,
          child: Text(text, style: Theme.of(context).textTheme.bodyText2.copyWith(fontSize: BaseButtonTheme.buttonTextSize, color: color)),
        ),
      ),
    );
  }

  void _onCloseButtonPress( BuildContext context ) {
    Navigator.pop(context);
    if (this.onCancelPress != null) this.onCancelPress();
  }

  void _onSureButtonPress( BuildContext context ) {
    if (this.onOKPress != null) this.onOKPress();
  }

}
